package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.MerMenu;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.mermenuform.InsertMerMenuForm;
import com.example.wechattakeaway.form.mermenuform.UpdateMerMenuForm;
import com.example.wechattakeaway.mapper.MerMenuMapper;
import com.example.wechattakeaway.service.MerMenuService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
public class MerMenuServiceImpl implements MerMenuService {
   @Autowired
    MerMenuMapper merMenuMapper;

    /**
     * 添加菜单
     * @param insertMerMenuForm
     * @return
     */
    @Override
    public ResultVO insertMerMenu(InsertMerMenuForm insertMerMenuForm) {
        MerMenu merMenu=new MerMenu();
        merMenu.setMenuName(insertMerMenuForm.getMenuName());
        merMenu.setMerchantId(insertMerMenuForm.getMerchantId());
        merMenu.setMenuDescription(insertMerMenuForm.getMenuDescription());
        try {

            merMenuMapper.insert(merMenu);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    /**
     * 编辑菜单
     * @param updateMerMenuForm
     * @return
     */
    @Override
    public ResultVO updateMerMenu(UpdateMerMenuForm updateMerMenuForm) {
        MerMenu merMenu=new MerMenu();
        merMenu.setMenuId(updateMerMenuForm.getMenuId());
        merMenu.setMenuName(updateMerMenuForm.getMenuName());
        merMenu.setMenuDescription(updateMerMenuForm.getMenuDescription());


        try {
            merMenuMapper.updateByPrimaryKeySelective(merMenu);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    /**
     * 根据id查询菜单
     * @param menuId
     * @return
     */
    @Override
    public ResultVO selectMenuById(Integer menuId) {
        MerMenu merMenu=new MerMenu();
        try {
           merMenu= merMenuMapper.selectByPrimaryKey(menuId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(merMenu);
    }

    /**
     *  根据商家id查询菜单列表（分页）
     * @param merchantId
     * @return
     */
    @Override
    public ResultVO selectMenuByMerchantId(Integer pageNum,Integer merchantId) {
        PageHelper.startPage(pageNum,5);

        List<MerMenu> list=new ArrayList<>();
        try {
            list= merMenuMapper.selectByMerchantId(merchantId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(list);
        return ResultVOUtil.success(hashMap);
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @Override
    public ResultVO deleteMerMenu(Integer menuId) {

        try {

            merMenuMapper.deleteByPrimaryKey(menuId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success();
    }

    /**
     * 根据商家id查询菜单列表
     * @param merchantId
     * @return
     */
    @Override
    public ResultVO selectMenuByMerId(Integer merchantId) {
        List<MerMenu> list=new ArrayList<>();

        try {
            list=merMenuMapper.selectByMerchantId(merchantId);

        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("list",list);
        return  ResultVOUtil.success(hashMap);
    }


}
